package pt.isel.pdm.yamba;

import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class TimelineActivity extends Activity implements Runnable, OnItemClickListener, OnSharedPreferenceChangeListener {
	private ListView list;
	private SharedPreferences prefs;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.timeline);
        list = (ListView) findViewById(R.id.timelineList);
		list.setOnItemClickListener(this);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        Global.timelineActivity = this;
        startService(Global.timelinePullService);
	}
	public void onSharedPreferenceChanged(SharedPreferences sp, String s) {
		try{
			if(s.equals("username") && !Global.username.equals(prefs.getString("username", null))){
				Global.username = prefs.getString("username", null);
				Global.resetTwitter();
				return;
			}
			if(s.equals("password") && !Global.password.equals(prefs.getString("password", null))){
				Global.password = prefs.getString("password", null);
				Global.resetTwitter();
				return;
			}
			if(s.equals("apiuri") && !Global.apiuri.equals(prefs.getString("apiuri", null))){
				Global.apiuri = prefs.getString("apiuri", null);
				Global.resetTwitter();
				return;
			}
			if(s.equals("autotlpull")){
				Global.autotlpull = prefs.getBoolean("autotlpull", true);
				synchronized(Global.timelinePullWait){
					Global.timelinePullWait.notify();
				}
			}
		}catch(Exception e){}
        Global.msgstoshow = Integer.parseInt(prefs.getString("maxmsg", "10"));
		Global.msgskept = Integer.parseInt(prefs.getString("msgskept", "10"));
		Global.pullInterval = Integer.parseInt(prefs.getString("pullinterval", "30"));
	}
	@Override
	public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
		Post post;
		try{
			post = Global.elems.get(pos);
		}catch(Exception e){
			return;
		}
		Intent details = new Intent(this, DetailActivity.class);
		Bundle e = new Bundle();
		e.putString("id", "" + post.get("id"));
		e.putString("author", "" + post.get("author"));
		e.putString("text", "" + post.get("fulltext"));
		e.putString("time", "" + post.get("fulltime"));
		details.putExtras(e);
		startActivity(details);
	}
	/** Initialize options menu */
	@Override
	public boolean onCreateOptionsMenu(Menu m) {
		getMenuInflater().inflate(R.menu.status, m);
		return true;
	}

	/** Process Item Menu selected */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
	    	case R.id.user:
	        	startActivity(new Intent(this, UserActivity.class));
	    		return true;
	    	case R.id.status:
	        	startActivity(new Intent(this, StatusActivity.class));
	    		return true;
	    	case R.id.preferences:
	        	startActivity(new Intent(this, PreferencesActivity.class));
	        	return true;
	    	case R.id.timeline:
	        	return true;
	    	case R.id.refresh:
	            startService(Global.timelinePullService);
	    		return true;
	    	default:
	    		return super.onOptionsItemSelected(item);
    	}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	@Override
	public void run() {
		try{
			List<Post> elems2 = new LinkedList<Post>();
			synchronized(Global.elems){
				for(int i = 0; i <Global.elems.size() && i < Global.msgstoshow; ++i)
					elems2.add(Global.elems.get(i));
			}
			SimpleAdapter adapter = new SimpleAdapter(TimelineActivity.this, elems2, R.layout.timeline_item, new String[]{"author", "text", "time"}, new int[]{R.id.idMessageAuthor, R.id.idMessageText, R.id.idMessageTime});
			list.setAdapter(adapter);
		}catch(Exception e){
			Toast.makeText(TimelineActivity.this, getString(R.string.apiError), Toast.LENGTH_LONG).show();
		}
	}
}
